<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二叉树的最大深度 - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            color: #d4d4d4;
        }
        .keyword { color: #c586c0; }
        .function { color: #dcdcaa; }
        .string { color: #ce9178; }
        .number { color: #b5cea8; }
        .comment { color: #6a9955; }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.1rem;
            color: #667eea;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="gradient-bg text-white py-20 px-6">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">
                二叉树的最大深度
            </h1>
            <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                探索递归思维的优雅之美，掌握树形结构的核心算法
            </p>
            <div class="mt-8 flex justify-center gap-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-code mr-2"></i>递归
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-project-diagram mr-2"></i>DFS
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-layer-group mr-2"></i>BFS
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Problem Description -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                    <i class="fas fa-book-open mr-3 text-gradient"></i>
                    题目描述
                </h2>
                <p class="text-lg text-gray-700 leading-relaxed">
                    <span class="drop-cap serif-font">给</span>定一个二叉树，找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。例如，给定二叉树 [3,9,20,null,null,15,7]，其最大深度为3（根节点为第1层，叶子节点15和7在第3层）。
                </p>
            </div>
        </section>

        <!-- Visual Representation -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                    <i class="fas fa-sitemap mr-3 text-gradient"></i>
                    二叉树结构可视化
                </h2>
                <div class="mermaid">
                    graph TD
                        A[3 - 根节点<br/>深度: 1] --> B[9<br/>深度: 2]
                        A --> C[20<br/>深度: 2]
                        C --> D[15<br/>深度: 3]
                        C --> E[7<br/>深度: 3]
                        style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                        style B fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#b794f4,stroke:#fff,stroke-width:2px,color:#fff
                        style E fill:#b794f4,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </section>

        <!-- Solution Approaches -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 text-center">
                解题思路
            </h2>
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Recursive Approach -->
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-sync-alt text-purple-600 text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">递归法</h3>
                    </div>
                    <p class="text-gray-700 mb-4 leading-relaxed">
                        最大深度为左右子树深度的最大值加 1。这是最直观和优雅的解法，体现了分治思想的精髓。
                    </p>
                    <div class="bg-purple-50 rounded-lg p-4 mt-4">
                        <p class="text-sm text-purple-800">
                            <i class="fas fa-clock mr-2"></i>时间复杂度：O(n)
                        </p>
                        <p class="text-sm text-purple-800 mt-2">
                            <i class="fas fa-memory mr-2"></i>空间复杂度：O(h)（h 为树高）
                        </p>
                    </div>
                </div>

                <!-- BFS Approach -->
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-layer-group text-blue-600 text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">BFS 法</h3>
                    </div>
                    <p class="text-gray-700 mb-4 leading-relaxed">
                        使用队列进行层序遍历，记录层数。适合需要逐层处理节点的场景。
                    </p>
                    <div class="bg-blue-50 rounded-lg p-4 mt-4">
                        <p class="text-sm text-blue-800">
                            <i class="fas fa-clock mr-2"></i>时间复杂度：O(n)
                        </p>
                        <p class="text-sm text-blue-800 mt-2">
                            <i class="fas fa-memory mr-2"></i>空间复杂度：O(w)（w 为最大宽度）
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Implementation -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 text-center">
                代码实现
            </h2>
            <div class="code-block">
                <div class="code-header">
                    <span class="code-dot bg-red-500"></span>
                    <span class="code-dot bg-yellow-500"></span>
                    <span class="code-dot bg-green-500"></span>
                    <span class="text-gray-400 ml-4 text-sm">Python</span>
                </div>
                <pre><code><span class="keyword">def</span> <span class="function">maxDepth</span>(root):
    <span class="keyword">if not</span> root:
        <span class="keyword">return</span> <span class="number">0</span>
    
    <span class="comment"># 递归计算左子树深度</span>
    left_depth = <span class="function">maxDepth</span>(root.left)
    
    <span class="comment"># 递归计算右子树深度</span>
    right_depth = <span class="function">maxDepth</span>(root.right)
    
    <span class="comment"># 返回较大深度 + 1（当前节点）</span>
    <span class="keyword">return</span> <span class="function">max</span>(left_depth, right_depth) + <span class="number">1</span></code></pre>
            </div>
        </section>

        <!-- Algorithm Flow -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                    <i class="fas fa-project-diagram mr-3 text-gradient"></i>
                    算法执行流程
                </h2>
                <div class="mermaid">
                    graph LR
                        A[开始] --> B{节点是否为空?}
                        B -->|是| C[返回 0]
                        B -->|否| D[计算左子树深度]
                        D --> E[计算右子树深度]
                        E --> F[取最大值 + 1]
                        F --> G[返回结果]
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style G fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#f56565,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </section>

        <!-- Key Insights -->
        <section class="mb-16">
            <div class="bg-gradient-to-r from-purple-50 to-blue-50 rounded-2xl p-8">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                    <i class="fas fa-lightbulb mr-3 text-yellow-500"></i>
                    核心要点
                </h2>
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-white rounded-lg p-6 text-center">
                        <i class="fas fa-brain text-4xl text-purple-600 mb-4"></i>
                        <h3 class="font-bold text-lg mb-2">递归思维</h3>
                        <p class="text-gray-600 text-sm">将复杂问题分解为相同的子问题，体现分治算法的精髓</p>
                    </div>
                    <div class="bg-white rounded-lg p-6 text-center">
                        <i class="fas fa-code-branch text-4xl text-blue-600 mb-4"></i>
                        <h3 class="font-bold text-lg mb-2">边界条件</h3>
                        <p class="text-gray-600 text-sm">空节点的深度为0，这是递归的终止条件</p>
                    </div>
                    <div class="bg-white rounded-lg p-6 text-center">
                        <i class="fas fa-chart-line text-4xl text-green-600 mb-4"></i>
                        <h3 class="font-bold text-lg mb-2">效率优化</h3>
                        <p class="text-gray-600 text-sm">每个节点只访问一次，时间复杂度达到最优</p>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <script>
        mermaid.initialize({ 
            startOnLoad: true,
            theme: 'default',
            themeVariables: {
                primaryColor: '#667eea',
                primaryTextColor: '#fff',
                primaryBorderColor: '#7c3aed',
                lineColor: '#5a67d8',
                secondaryColor: '#9f7aea',
                tertiaryColor: '#b794f4'
            }